package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;

import java.io.File;
import java.util.List;

/**
 * 文件选择页面的适配器
 * @author Mr.Lee
 *
 */
public class FileDirListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Bitmap mFolder,mFile;
    private List<String> paths;
    private int type;
    private Context mContext;

    public FileDirListAdapter(Context context, List<String> paths, int type) {
        this.mInflater = LayoutInflater.from(context);
        this.paths = paths;
        this.mFolder = BitmapFactory.decodeResource(context.getResources(), R.mipmap.folder);
        this.mFile = BitmapFactory.decodeResource(context.getResources(), R.mipmap.file_list);
        this.type=type;
        this.mContext=context;
    }

    public int getCount() {
        return paths == null?0:paths.size();
    }

    public Object getItem(int position) {
        return paths.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.file_select_item, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.file_select_item_text);
            holder.icon = (ImageView) convertView.findViewById(R.id.file_select_item_image);
            holder.linearLayout=(LinearLayout)convertView.findViewById(R.id.file_select_item_lin);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (type== AppConfig.FILE_TYPE_FILE_DIRE) {
        	holder.icon.setImageBitmap(mFolder);
        	holder.text.setText(new File(paths.get(position).toString()).getName());
		}
        if (type ==AppConfig.FILE_TYPE_FILE) {
        	
        	if (type == AppConfig.FILE_TYPE_FILE) {
        		holder.text.setText(new File(paths.get(position).toString()).getName());
        		if (new File(paths.get(position)).isDirectory()) {
        			holder.icon.setImageBitmap(mFolder);
        		}
        		else {
        			holder.icon.setImageBitmap(mFile);
        		}
        	}
			
		}
        /*if (position==0){
            holder.linearLayout.setBackground(mContext.getResources().getDrawable(R.drawable.mouse_list_top_bg));
        }
        if (position==paths.size()){
            holder.linearLayout.setBackground(mContext.getResources().getDrawable(R.drawable.mouse_list_bootom_bg));
        }*/
        return convertView;
    }

    private class ViewHolder {
        TextView text;
        ImageView icon;
        LinearLayout linearLayout;
    }

}
